package org.dromara.system.service.impl;

import org.dromara.system.service.SysRoleDeptService;
import org.dromara.system.pojo.entity.SysRoleDeptDO;
import org.dromara.system.pojo.bo.SysRoleDeptBO;
import org.dromara.system.pojo.qo.SysRoleDeptQO;
import org.dromara.system.pojo.vo.SysRoleDeptVO;
import org.dromara.system.mapper.SysRoleDeptMapper;
import org.springframework.stereotype.Service;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.BeanCopyUtils;

import java.util.List;

/**
 * 角色和部门关联表 服务层实现。
 *
  * @author mkj
  * @since 2024-07-21
 */
@RequiredArgsConstructor
@Service
public class SysRoleDeptServiceImpl  implements SysRoleDeptService {

    private final SysRoleDeptMapper sysRoleDeptMapper;

    @Override
    public boolean add(SysRoleDeptBO sysRoleDeptBO) {
        SysRoleDeptDO sysRoleDeptDO = BeanCopyUtils.copy(sysRoleDeptBO, SysRoleDeptDO.class);
        return sysRoleDeptMapper.insert(sysRoleDeptDO) > 0;
    }

    @Override
    public boolean remove(Long id) {
        return sysRoleDeptMapper.deleteById(id) > 0;
    }

    @Override
    public boolean update(SysRoleDeptBO sysRoleDeptBO) {
        SysRoleDeptDO sysRoleDeptDO = BeanCopyUtils.copy(sysRoleDeptBO, SysRoleDeptDO.class);
        return sysRoleDeptMapper.update(sysRoleDeptDO) > 0;
    }

    @Override
    public List<SysRoleDeptVO> list(SysRoleDeptQO sysRoleDeptQO) {
        SysRoleDeptDO sysRoleDeptDO = BeanCopyUtils.copy(sysRoleDeptQO, SysRoleDeptDO.class);
        QueryWrapper queryWrapper = QueryWrapper.create(sysRoleDeptDO);
        return sysRoleDeptMapper.selectListByQueryAs(queryWrapper, SysRoleDeptVO.class);
    }

    @Override
    public SysRoleDeptVO getInfo(@PathVariable Long id) {
        SysRoleDeptDO sysRoleDeptDO = sysRoleDeptMapper.selectOneById(id);
        if (sysRoleDeptDO == null){
            return null;
        }
        return BeanCopyUtils.copy(sysRoleDeptDO, SysRoleDeptVO.class);
    }

    @Override
    public Page<SysRoleDeptVO> page(SysRoleDeptQO page) {
        return sysRoleDeptMapper.paginateAs(page.getPageNumber(), page.getPageSize(), QueryWrapper.create(page), SysRoleDeptVO.class);
    }

}

